#!/usr/bin/make -f

BRANCH ?= `git rev-parse --abbrev-ref HEAD`
TAG = `git tag | sort -V | tail -1`
COMMIT = `git rev-parse --short HEAD`
DATE = `date`
DOCKER ?= docker
IMAGE_NAME ?= registry.cn-zhangjiakou.aliyuncs.com/xiaoduoai/agent-cloud-knowledge_base:${BRANCH}
INJECT_PKG := gitlab.xiaoduoai.com/agent-cloud/knowledge_base/cmd

.PHONY: all
all: package

.PHONY: test
test:
	go test -tags=unit -timeout 30s -short `go list ./...  | grep -v /vendor/`

.PHONY: build
build: 
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
	-ldflags "-s -w -X '${INJECT_PKG}.Version=${TAG}' -X ${INJECT_PKG}.Commit=${COMMIT} -X '${INJECT_PKG}.Date=${DATE}'" \
	-o ./dist/knowledge_base .

.PHONY: package
package:
	${DOCKER} build --platform linux/amd64 \
		--build-arg GOPROXY=${GOPROXY} \
		--build-arg GOPRIVATE=${GOPRIVATE} \
		--build-arg VERSION=${TAG} \
		--build-arg COMMIT=${COMMIT} \
		--build-arg BUILD_DATE=${DATE} \
		-t ${IMAGE_NAME} \
		-f ./build/app/Dockerfile .
	${DOCKER} push ${IMAGE_NAME}

.PHONY: clean	
clean:
	rm -rf ./dist;

.PHONY: swagger
swagger:
	swag init -g doc.go

.PHONY: lint
lint:
	@echo "check lint"
	@GO111MODULE=on go get -d github.com/golangci/golangci-lint/cmd/golangci-lint@v1.29.0
	@golangci-lint run --config ./golangci.yml --timeout 10000s

.PHONY: proto-gen
#proto-gen: install-tools
proto-gen: 
	# this ensure no error hanppens when run protoc generate
	go generate ./...

.PHONY: proto
proto:
	protoc --go_out=paths=source_relative:. $(wildcard proto/*.proto)


.PHONY: sync
sync:
	cp -r proto sdk;
	grep "/ecrobot/knowledge_base/proto" -rl ./sdk/proto | xargs sed -i "s/\/ecrobot\/knowledge_base\/proto/\/ecrobot\/knowledge_base\/sdk\/proto/g";
#	# grep "/base/knowledge_base/proto" -rl ./sdk/proto | xargs sed -i "s/\/base\/knowledge_base\/proto/\/base\/knowledge_base\/sdk\/proto/g";
#
#.PHONY: sync-mac
#sync-mac:
#	cp -r proto sdk;
#	grep "/ecrobot/knowledge_base/proto" -rl ./sdk/proto | xargs sed -i "" "s/\/ecrobot\/knowledge_base\/proto/\/ecrobot\/knowledge_base\/sdk\/proto/g";
#	# grep "/base/knowledge_base/proto" -rl ./sdk/proto | xargs sed -i "" "s/\/base\/knowledge_base\/proto/\/base\/knowledge_base\/sdk\/proto/g";
